//
//  SettingViewController.m
//  edu
//
//  Created by baobaodp on 13-8-25.
//  Copyright (c) 2013年 baobaodp. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingCell.h"

#import "CommonTableViewController.h"
#import "UpdateFieldViewController.h"
#import "EduViewController.h"
#import "Record.h"

@interface SettingViewController ()

@end

@implementation SettingViewController

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Custom initialization
        self.isODRefresh = FALSE;
        self.isWithZoom = FALSE;
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.isODRefresh = FALSE;
        self.isWithZoom = FALSE;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.datas = [NSMutableArray arrayWithObjects:@"雨松",@"雨松",@"雨松",@"雨松",@"雨松",@"雨松",@"雨松",@"雨松",@"雨松", @"MOMO",@"哇咔咔",nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark setTitle

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{
    return 30;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{
    
    CGRect frameRect = CGRectMake(10, 0, 160, 40);
    
    UILabel *label = [[UILabel alloc] initWithFrame:frameRect];
    
    if(section == 0){
        label.text=@"  帐号";

    }else if(section == 1){
        label.text=@"  学校信息";

    }else if(section == 2){
        label.text=@"  个人信息";
    }

    return label;
 
}
#pragma mark Table Data Source Methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.datas count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSUInteger row = [indexPath row];
    NSUInteger sectionIndex = [indexPath section];
    
    Record *data = [[self.datas objectAtIndex:sectionIndex] objectAtIndex:row];

    if([[data.fields allKeys] containsObject:@"height"]){
        return [[data getFieldValueByName:@"height"] intValue];
    }
    
    //default
    return 50;
}
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{

    return [[self.datas objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"ScoreCellIdentifier";
    
    SettingCell *cell = (SettingCell *)[tableView
                                    dequeueReusableCellWithIdentifier: cellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SettingCell"
                                                     owner:self options:nil];
        for (id oneObject in nib)
            if ([oneObject isKindOfClass:[SettingCell class]])
                cell = (SettingCell *)oneObject;
    }
	
    NSUInteger row = [indexPath row];
    NSUInteger sectionIndex = [indexPath section];
    
    Record *data = [[self.datas objectAtIndex:sectionIndex] objectAtIndex:row];
    [cell setData:data index:row];
    
    //含有field标识显示详细按钮
    if([[data.fields allKeys] containsObject:@"field"]){
        [self configureCell:cell indexPath:indexPath accessoryType:DISCLOSURE_INDICATOR];
    }
    
    return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSUInteger row = [indexPath row];
    NSUInteger sectionIndex = [indexPath section];
    Record *data = [[self.datas objectAtIndex:sectionIndex] objectAtIndex:row];
    
    //含有field标识打开编辑画面，否则返回
    if([[data.fields allKeys] containsObject:@"field"] == false){
        return ;
    }
    
    // Navigation logic may go here. Create and push another view controller.
	
	UpdateFieldViewController *detailViewController = [[UpdateFieldViewController alloc] initWithNibName:@"UpdateFieldViewController" bundle:nil];
    
    detailViewController.primaryValue =[data getFieldValueByName:@"value"];
    detailViewController.fieldName =[data getFieldValueByName:@"field"];
    
	// Pass the selected object to the new view controller.
	
	if (tableView == self.searchDisplayController.searchResultsTableView){
        //		detailViewController.currentLineName = [filteredListContent objectAtIndex:indexPath.row];
        //		detailViewController.currentLineIndex = [[CDataContainer Instance].lineNameArray indexOfObject:[filteredListContent objectAtIndex:indexPath.row]];
	}
	else{
        //		detailViewController.currentLineName = [[CDataContainer Instance].lineNameArray objectAtIndex:indexPath.row];
        //		detailViewController.currentLineIndex = indexPath.row;
	}
    
    detailViewController.updateFieldDelegate = self;
	[self.navigationController pushViewController:detailViewController animated:YES];
    
}


#pragma mark -
#pragma mark UINavigationController delegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [self.mainTableView reloadData];
}

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated {
    [self.mainTableView reloadData];
}

-(void) viewWillAppear:(BOOL)animated
{
    [self getRecords];
    [self.mainTableView reloadData];
}

-(BOOL) upDataField:(NSString *) fieldName value:(NSString *) value
{
    [userEntry.userInfo addField:fieldName value:value];
    [userEntry.userInfo updateField:fieldName];
    
    return true;
}

/*
   获取数据
 */
-(void)getRecords
{
    self.datas  = [[NSMutableArray alloc]init];
    
    //－－－－－－ 学生信息：
    
    //section1:头头像等基本信息
    NSMutableArray *baseInfo = [[NSMutableArray alloc]init];
    
    //头像
    Record *img = [[Record alloc]initWithTableName:@"temp"];
    [img addField:@"title" value:@"头像"];
    [img addField:@"height" value:@"80"];
    [img addField:@"img" value:[userEntry.userInfo getFieldDataByName:@"stu_Img"]];
    [baseInfo addObject:img];
    
    //头像
    Record *name = [[Record alloc]initWithTableName:@"temp"];
    [name addField:@"title" value:@"姓名"];
    [name addField:@"value" value:[userEntry.userInfo getFieldDataByName:@"stu_Name"]];
    [baseInfo addObject:name];
    
    [self.datas addObject:baseInfo];
    
    //section2:学校信息
    NSMutableArray *schInfo = [[NSMutableArray alloc]init];
    
    //学校
    Record *sch = [[Record alloc]initWithTableName:@"temp"];
    [sch addField:@"title" value:@"学校"];
    [sch addField:@"value" value:[userEntry.userInfo getFieldDataByName:@"sch_Name"]];
    [schInfo addObject:sch];
    
    //班级
    Record *classNo = [[Record alloc]initWithTableName:@"temp"];
    [classNo addField:@"title" value:@"班级编号"];
    [classNo addField:@"value" value:[userEntry.userInfo getFieldDataByName:@"class_No"]];
    [schInfo addObject:classNo];
    
    Record *className = [[Record alloc]initWithTableName:@"temp"];
    [className addField:@"title" value:@"班级名称"];
    [className addField:@"value" value:[userEntry.userInfo getFieldDataByName:@"class_Name"]];
    [schInfo addObject:className];

    
    [self.datas addObject:schInfo];
    
    //section2:联系方式等信息
    NSMutableArray *accessInfo = [[NSMutableArray alloc]init];
    
    //电话
    Record *tel = [[Record alloc]initWithTableName:@"temp"];
    [tel addField:@"title" value:@"电话"];
    [tel addField:@"value" value:[userEntry.userInfo getFieldDataByName:@"stu_Tel"]];
    [tel addField:@"field" value:@"stu_Tel"];
    [accessInfo addObject:tel];
    
    //地址
    Record *addr = [[Record alloc]initWithTableName:@"temp"];
    [addr addField:@"title" value:@"地址"];
    [addr addField:@"value" value:[userEntry.userInfo getFieldDataByName:@"stu_Address"]];
    [addr addField:@"field" value:@"stu_Address"];
    [accessInfo addObject:addr];
    
    [self.datas addObject:accessInfo];

    
    //section3:地区，学校，班级信息
}

@end
